package com.tianhui.consignortwofei.mvp.ui.fragment.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;

import com.fgs.common.utils.CheckUtil;
import com.fgs.common.utils.CountDownTimerUtil;
import com.fgs.common.widget.TextChange;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.interf.OnLoginFragmentSwitchListener;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.contract.LoginContract;
import com.tianhui.consignortwofei.mvp.model.enty.CaptchaInfo;
import com.tianhui.consignortwofei.mvp.model.enty.LoginInfo;
import com.tianhui.consignortwofei.mvp.presenter.LoginPresenter;
import com.tianhui.consignortwofei.mvp.ui.activity.LoginActivityNew;
import com.tianhui.consignortwofei.mvp.ui.activity.MainActivity;
import com.tianhui.consignortwofei.mvp.ui.activity.OnlyLookQuotListTwoActivity;
import com.xuexiang.xui.widget.edittext.ClearEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/11
 * @description
 **/
public class CodeFragment extends BaseFragment<LoginContract.View, LoginContract.Presenter> implements LoginContract.View, OnLoginFragmentSwitchListener {

    @BindView(R.id.fragment_code_phoneEditText)
    ClearEditText phoneEditText;
    @BindView(R.id.fragment_code_codeEditText)
    ClearEditText codeEditText;
    @BindView(R.id.fragment_code_codeTextView)
    TextView codeTextView;
    @BindView(R.id.fragment_code_loginButton)
    Button loginButton;




    private boolean isChecked = true;
    private CountDownTimerUtil mCountDownTimerUtil;
    //密码登录时的账户名称
    private String accountName;

    public CodeFragment() {
    }

    @Override
    protected boolean isNeedLazyLoad() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_code;
    }

    @Override
    protected LoginContract.View createView() {
        return this;
    }

    @Override
    protected LoginContract.Presenter createPresenter() {
        return new LoginPresenter(getContext());
    }

    @Override
    protected void init() {
        new TextChange.Builder()
                .addEditText(phoneEditText, 11)
                .addEditText(codeEditText, 6)
                .addListenView(loginButton)
                .build();


    }



    @Override
    public void result(LoginInfo info) {
        SharedPreferenceManager.getInstance().setToken(info.token);

        String phone = phoneEditText.getText().toString().trim();
        if ("18833838825".equals(phone)){
            //报价列表
            Bundle bundle=new Bundle();
            bundle.putInt("lookType",2);
            openActivity(OnlyLookQuotListTwoActivity.class,bundle);
        }else {
            openActivity(MainActivity.class);
        }
        getActivity().finish();
    }

    @Override
    public void authCodeResult() {
        startCountDownTime();
    }

    private void startCountDownTime() {
        mCountDownTimerUtil = new CountDownTimerUtil(codeTextView, 1000 * 60, 1000);
        mCountDownTimerUtil.start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mCountDownTimerUtil != null) {
            mCountDownTimerUtil.timeCancel();
        }
    }

    @OnClick(R.id.fragment_code_codeTextView)
    public void getAuthCode() {

//        String phone = phoneEditText.getText().toString().trim();
//        wv_captcha.setVisibility(View.VISIBLE);

//        new AuthCode(getContext(), phone).authCode(new AuthCode.OnAuthCodeCallback() {
//            @Override
//            public void onAuthCallback(CaptchaInfo info) {
//                if (info.ret == 0) {
//                    info.mobile = "+86" + phone;
//                    info.action = "login";
//                    getPresenter().getAuthCode(info, true);
//                }
//            }
//        });
        hideKeyboard();
        LoginActivityNew loginActivityNew = (LoginActivityNew) getActivity();
        if (loginActivityNew != null) {
            loginActivityNew.getYanZheng();
        }

    }


    @OnClick(R.id.fragment_code_loginButton)
    public void login() {

        hideKeyboard();

        String phone = phoneEditText.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        }

        if (!CheckUtil.isMobileNO(phone)) {
            showToast("请输入正确的手机号");
            return;
        }
        String code = codeEditText.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            showToast("请输入验证码");
            return;
        }

        if (code.length() < 6) {
            showToast("验证码不能少于6位");
            return;
        }


        if (!isChecked) {
            showToast("请先同意用户协议和隐私政策");
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("mobile", "+86" + phone);
        map.put("username", phone);
        map.put("password", "password");//随便写但不能不传
        map.put("code", code);
        map.put("type", "1,2");
        getPresenter().login(map, true);
    }

    @Override
    public void onFragmentSwitch(String phone) {
        //账号密码登录时，若账号不是手机号，切换登录方式时不需要赋默认值
        accountName = phone;
        if (phoneEditText != null) {

            if (CheckUtil.isMobileNO(phone)) {
                phoneEditText.setText(phone);
                if (!TextUtils.isEmpty(phone)) {
                    phoneEditText.setSelection(phone.length());
                }
            }

        }
    }

    @Override
    public String getPhone() {
        if (phoneEditText != null) {

            String phone = phoneEditText.getText().toString().trim();
            if (!TextUtils.isEmpty(phone) && CheckUtil.isMobileNO(phone)) {
                return phone;
            } else {
                return accountName;
            }
        }
        return "";
    }

    @Override
    public void setIsChecked(boolean isChecked) {
        this.isChecked = isChecked;
    }





    public void sendYanZheng(CaptchaInfo info) {
//                CaptchaInfo info = JsonUtil.fromJson(data, new TypeToken<CaptchaInfo>() {
//                }.getType());
                if (info.ret == 0) {
                    String phone = phoneEditText.getText().toString().trim();
                    info.mobile = "+86" + phone;
                    info.action = "login";
                    getPresenter().getAuthCode(info, true);
                }
    }








}
